
#ifndef TOKZRGENTR_UTILS_UCS2_RANGES_H
#define TOKZRGENTR_UTILS_UCS2_RANGES_H
#include<set>
#include"asserts.h"
namespace TokzrGentr {
	//#	Representing a unicode code point range(UCS-2 only,basically coverred BMP).
	//#	e.g. \u0 - \u30 means all code point between 0 and 30.
	//#	Specially,let single code point as a ucs2_range ,too.
	//#	e.g. code point 'a' as \u97 - \u97.
	class ucs2_range
	{
	private:
		char16_t _lo;
		char16_t _hi;
	public:
		ucs2_range(char16_t ch)
			:_lo(ch), _hi(ch) {
		}
		ucs2_range(char16_t lo, char16_t hi)
			:_lo(lo), _hi(hi) {
			ASSERT(lo <= hi);
		}
		char16_t low() const { return _lo; }
		char16_t high() const { return _hi; }
		bool is_contain(char16_t ch) const { return _lo <= ch && _hi >= ch; }
		bool is_single() const {
			return _lo == _hi;
		}
		friend bool operator==(const ucs2_range &lhs, const ucs2_range &rhs)
		{
			return lhs.low() == rhs.low() && lhs.high() == rhs.high();
		}
		friend bool operator<(const ucs2_range &lhs, const ucs2_range &rhs)
		{
			return lhs.low() < rhs.low() || (lhs.low() == rhs.low() && lhs.high() < rhs.high());
		}
	};

	//#	Representing a chars set.
	//#	e.g. [a-zA-Z0-9] [_] ect.
	class ucs2_ranges_set
	{
	public:
		using iterator = std::set<ucs2_range>::iterator;
	private:
		std::set<ucs2_range> _set;
		bool _sign;//True for [a-z],False for [^a-z]
	public:
		ucs2_ranges_set(bool sign)
			:_sign(sign) {}
		bool is_positive() const
		{
			return _sign;
		}
		bool is_contain(char16_t ch) const
		{
			for (auto range : _set)
				if (range.is_contain(ch))
					return _sign;
			return !_sign;
		}
		ucs2_ranges_set& add_range(const ucs2_range &range)
		{
			_set.insert(range);
			return *this;
		}
		ucs2_ranges_set& del_range(const ucs2_range &range)
		{
			_set.erase(range);
			return *this;
		}
		std::set<ucs2_range> ucs2_ranges() const
		{
			return _set;
		}
		iterator begin() const
		{
			return _set.begin();
		}
		iterator end() const
		{
			return _set.end();
		}
		void set_sign(bool sign)
		{
			_sign = sign;
		}
	};
}
#endif // !TOKZRGENTR_UTILITYS_USC2_RANGES_H

